#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pa ./ebin

-module(changes).

main(_) ->
    couchbeam:start(),
    Server = couchbeam:server_connection(),
    {ok, Db} = couchbeam:open_or_create_db(Server, "testdb"),
    {ok, Ref} = couchbeam_changes:follow(Db, [continuous, heartbeat]),
    io:format("stream ref ~p~n", [Ref]),
    io:format("i am ~p~n", [self()]),
    get_changes(Ref).


get_changes(Ref) ->
    receive
        {Ref, {done, LastSeq}} ->
            io:format("stopped, last seq is ~p~n", [LastSeq]),
            ok;
        {Ref, {change, Change}} ->
            io:format("got ~p ~n", [Change]),
            get_changes(Ref);
        {Ref, Else}->
            io:format("else ? ~p ~n", [Else])
    end.
